<!-- 权限管理页面 -->
<template>
  <div>
    <el-form v-model="searchMap" :inline="true">
      <el-form-item label="模块名称">
        <el-input v-model="searchMap.moduleName" placeholder="请输入"></el-input>
      </el-form-item>

      <el-form-item label="表名">
        <el-input v-model="searchMap.tableName" placeholder="请输入"></el-input>
      </el-form-item>

      <el-form-item label="创建人">
        <el-input v-model="searchMap.creatorId" placeholder="请输入"></el-input>
      </el-form-item>

      <el-button type="primary" @click="fetchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column prop="moduleName" label="模块名称" width="80">
      </el-table-column>
      <el-table-column prop="tableName" label="表名" width="80">
      </el-table-column>
      <el-table-column prop="permissionName" label="权限名" width="120">
      </el-table-column>
      <el-table-column prop="tag" label="标记" width="160"> </el-table-column>
      <el-table-column prop="creatorName" label="创建者" width="160">
      </el-table-column>
      <el-table-column prop="created" label="创建时间" :formatter="dataFormat" width="160">
      </el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination @size-change="sizeData" @current-change="pageData" :page-sizes="[5, 10, 20, 30, 60]" :page-size="5"
      layout="total, sizes, prev, pager, next, jumper" :total="count">
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-row>
          <el-col span="11">
            <el-form-item label="所属表">
              <el-input v-model="permission.tableName" />
            </el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="所属模块">
              <el-input v-model="permission.moduleName" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="11">
            <el-form-item label="权限名称"><el-input v-model="permission.permissionName" /></el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="标记"><el-input v-model="permission.tag" /></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="11">
            <el-form-item label="创建人">
              <el-select v-model="permission.creatorId">
                <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="创建时间">
              <el-col :span="11">
                <el-date-picker type="date" placeholder="选择日期" v-model="permission.created"></el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="11">
            <el-form-item label="备注"><el-input type="textarea" v-model="permission.remark" /></el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="handleSave()" style="margin-left: 240px">保存</el-button>
          <el-button @click="dialogFormVisible = false" style="margin-left: 10px">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import permissionService from "@/api/permission.js";
import userService from "@/api/user";
//导入日期组件
import moment from "moment";

export default {
  data() {
    return {
      tableData: [],
      userList:[],
      dialogFormVisible: false,
      id: "",
      permission: {},
      searchMap: {
        limit: 5,
        page: 1,
        moduleName: "",
        tableName: "",
        creatorId: ""
      },
      //记录总数
      count: 0,
    };
  },
  //钩子函数
  created() {
    this.findAllUser();
    this.fetchData();
  },
  methods: {
    //修改或新增
    handleEdit(id) {
      this.id = id;

      //打开弹窗
      this.dialogFormVisible = true;

      if (id !== "") {
        //根据id查询数据绑定到模型上面
        permissionService.find(id).then((response) => {
          //回显
          this.permission = response.data;
        });
      } else {
        //清除之前模型的脏数据
        this.permission = {};
      }
    },

    //新增和修改的提交
    handleSave() {
      permissionService.update(this.id, this.permission).then((response) => {
        //新增或者修改成功
        this.$message({
          message: response.msg,
          type: "success",
        });

        //重新加载列表的数据
        this.fetchData();

        //关闭弹窗
        this.dialogFormVisible = false;
      });
    },

    //通过创建人
    findAllUser(){
      userService.findAll().then(response=>{
        this.userList=response.data
      })
    },

    
    //查询数据
    fetchData() {
      const data = this.searchMap;
      permissionService.list(data).then((response) => {
        this.tableData = response.data;
        this.count = response.count;
      });
    },
    //当页面大小改变的时候调用此方法，传递改变的页面大小
    sizeData(size) {
      this.searchMap.limit = size;
      this.fetchData();
    },
    pageData(page) {
      this.searchMap.page = page;
      this.fetchData();
    },

    //增加一个格式化日期的方法
    dataFormat(row, column) {
      let date = row[column.property];

      if (date == undefined) {
        return "";
      }

      return moment(date).format("YYYY-MM-DD hh:mm:ss");
    },

    //删除
    handleDelete(id) {
      this.$confirm("您确定要删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          permissionService.remove(id).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            this.fetchData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
